﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WoChu.RfClient.Entity;
using WoChu.RfClient.Common;
using WoChu.CF.BaseFrame.Util;

namespace WoChu.RfClient.Duty
{
    public partial class FrmInventoryAdjustment : Form
    {
        public FrmInventoryAdjustment()
        {
            InitializeComponent();
        }

        private static int iSort = 0;
        //当前货位编码
        private static List<LocationInventoryInfo> lstAdjustment = null;
        private static List<LocationInventoryInfo> lstInventory = null;
        private static List<LocationInventoryInfo> _lstListStock = null;
        
        public static List<LocationInventoryInfo> lstListStock
        {
            get
            {
                if (_lstListStock == null) _lstListStock = new List<LocationInventoryInfo>();
                return _lstListStock;
            }
        }

        private void FrmInventoryAdjustment_Load(object sender, EventArgs e)
        {
            lstInventory = Duty.InventoryAdjustmentUIRule.NewInstance.GetLocationInventoryInfo(LoginBean.Instance.USERCODE);
        
            if (lstInventory == null || lstInventory.Count == 0)
            {
                MessageBox.Show("获取库存数据失败，请重新打开窗体！");
                return;
            }
            txtLocation.Focus();
        }
        
        private void txtLocation_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;
            string ltcode = this.txtLocation.Text.Trim();
            if (string.IsNullOrEmpty(ltcode))
            {
                MessageBox.Show("扫描货位号不能为空！");
            }
            txtBarCode.Focus();
            txtBarCode.SelectAll();
        }
  
        private void txtBarCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;
            string ltCode = this.txtLocation.Text.Trim();
            if (string.IsNullOrEmpty(ltCode))
            {
                MessageBox.Show("扫描货位不能为空！");
                return;
            }
            string barCode = this.txtBarCode.Text.Trim();
            if (string.IsNullOrEmpty(barCode))
            {
                MessageBox.Show("扫描大条码不能为空！");
                return;
            }
            try
            {
                string barGoodCode = null;
                if ((barCode.Substring(0, 1) == "0" && barCode.Length == 10) || barCode.Substring(0, 3) == "WOC" && (barCode.Length == 9 || barCode.Length == 12))
                {
                    barGoodCode = barCode;
                }
                else if ((barCode.Length == 18 && barCode.Substring(0, 1) == "0") || ((barCode.Length == 17 || barCode.Length == 20) && barCode.Substring(0, 3) == "WOC"))
                {
                    barGoodCode = barCode.Substring(0, barCode.Length - 8);
                }
                else
                {
                    MessageBox.Show("请商品正确的‘0’条码 或‘WOC’条码！");
                    return;
                }
                lstAdjustment = lstInventory.FindAll(x => x.LOCATIONCODE == ltCode && x.GOODSCODE == barGoodCode);

                this.adjustmentSource.DataSource = null;
                this.adjustmentSource.DataSource = lstAdjustment;
                this.dbGridAdjustment.DataSource = this.adjustmentSource;
                lstAdjustment.Sort((x, y) =>
                {
                    return x.GOODSBATCHCODE.CompareTo(y.GOODSBATCHCODE);
                });
                this.dbGridAdjustment.Refresh();
            }
            catch(Exception ex)
            {
                txtBarCode.Focus();
                txtBarCode.SelectAll();
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex);
            } 
        }

        private void txtQty_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;
            if (lstAdjustment == null)
            {
                this.txtQty.Text = string.Empty;
                this.txtQty3.Text = string.Empty;
                this.txtBarCode.Text = string.Empty;
                this.txtBarCode.Focus();
                this.txtBarCode.SelectAll();
            }
            string _txtQty = this.txtQty.Text.Trim();

            if (!string.IsNullOrEmpty(_txtQty))
            {
                decimal _Qty = 0;
                try
                {
                    _Qty = Convert.ToDecimal(_txtQty);
                }
                catch
                {
                    WoChu.CF.BaseFrame.Util.MessageBoxEx.Show("输入的数量不正确!");
                    this.txtQty.SelectAll();
                    return;
                }
                if (_Qty < 0)
                {
                    WoChu.CF.BaseFrame.Util.MessageBoxEx.Show("输入的数量不能小于0!");
                    this.txtQty.SelectAll();
                    return;
                }

                int curIndex = dbGridAdjustment.CurrentRowIndex;
                if (curIndex < 0)
                {
                    MessageBox.Show("请选选择的数据行！");
                    return;
                }

                lstAdjustment[curIndex].QTY = _Qty;
                this.txtQty3.Text = string.Empty;
                this.txtQty3.Focus(); 
                //if (!lstListStock.Contains(curItem))
                //{
                //    lstListStock.Add(curItem);
                //}

                //this.adjustmentSource.DataSource = null;
                //this.adjustmentSource.DataSource = lstAdjustment;
                //this.dbGridAdjustment.DataSource = this.adjustmentSource;
                //this.dbGridAdjustment.Refresh();

                ////设置已扫描的为加亮
                //SelectionBright(curItem.GOODSBATCHCODE);

                //this.lblQtySum.Text = lstAdjustment.Sum(o => o.QTY).ToString();
                //this.txtQty.Text = string.Empty;
                
            }
        }

        private void txtQty3_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;
            if (lstAdjustment == null)
            {
                this.txtQty.Text = string.Empty;
                this.txtQty3.Text = string.Empty;
                this.txtBarCode.Text = string.Empty;
                this.txtBarCode.Focus();
                this.txtBarCode.SelectAll();
            }
            string _txtQty3 = this.txtQty3.Text.Trim();

            if (!string.IsNullOrEmpty(_txtQty3))
            {
                decimal _Qty3 = 0;
                try
                {
                    _Qty3 = Convert.ToDecimal(_txtQty3);
                }
                catch
                {
                    WoChu.CF.BaseFrame.Util.MessageBoxEx.Show("输入的数量不正确!");
                    this.txtQty3.SelectAll();
                    return;
                }
                if (_Qty3 < 0)
                {
                    WoChu.CF.BaseFrame.Util.MessageBoxEx.Show("输入的数量不能小于0!");
                    this.txtQty3.SelectAll();
                    return;
                }
                int curIndex = dbGridAdjustment.CurrentRowIndex;
                if (curIndex < 0)
                {
                    MessageBox.Show("请选选择的数据行！");
                    return;
                }
                
                LocationInventoryInfo curItem = lstAdjustment[curIndex];
                if (curItem.GOODSID3 > 0)
                {
                    curItem.QTY3 = _Qty3;
                }
                else
                {
                    if (_Qty3 > 0)
                    {
                        MessageBox.Show("该商品没有小单位，请输入‘0’！");
                        this.txtQty3.SelectAll();
                        return;
                    }
                }

                if (!lstListStock.Contains(curItem))
                {
                    lstListStock.Add(curItem);
                }

                this.adjustmentSource.DataSource = null;
                this.adjustmentSource.DataSource = lstAdjustment;
                this.dbGridAdjustment.DataSource = this.adjustmentSource;
                this.dbGridAdjustment.Refresh();

                //设置已扫描的为加亮
                SelectionBright(curItem.GOODSBATCHCODE);

                //this.lblQtySum.Text = lstAdjustment.Sum(o => o.QTY).ToString();
                this.txtQty3.Text = string.Empty;
                this.txtBarCode.Text = string.Empty;
                this.txtBarCode.Focus();
                this.txtBarCode.SelectAll();
            }
        }

        private void dbGridAdjustment_DoubleClick(object sender, EventArgs e)
        {
            int curIndex = dbGridAdjustment.CurrentRowIndex;
            if (curIndex < 0) return;

            LocationInventoryInfo curItem = lstAdjustment[curIndex];
            txtLocation.Text = curItem.LOCATIONCODE;
            txtBarCode.Text = curItem.GOODSBATCHCODE;
            SelectionBright(curItem.GOODSBATCHCODE);
            txtQty.Text = string.Empty;
            txtQty3.Text = string.Empty;
            txtQty.Focus();
            txtQty.SelectAll();
        }

        private void dbGridListStock_DoubleClick(object sender, EventArgs e)
        {
            int curIndex = dbGridListStock.CurrentRowIndex;
            if (curIndex < 0) return;
            try
            {

                LocationInventoryInfo curItem = lstListStock[curIndex];

                string ltCode = curItem.LOCATIONCODE;
                string gbName = curItem.GOODSNAME;

                string Message = string.Format("货位:{0}, 名称:{1}, \n 是否删除该数据？", ltCode, gbName);
                if (MessageBoxEx.Question(Message) == DialogResult.Yes)
                {
                    lstListStock.Remove(curItem);
                    this.listStockSource.DataSource = null;
                    this.listStockSource.DataSource = lstListStock;
                    this.dbGridListStock.DataSource = this.listStockSource;
                    this.dbGridListStock.Refresh();
                }
            }
            catch (Exception ex)
            {
                TraceEx.Write(ex.Message);
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show("删除数据失败!");
            }
            finally
            {
                txtBarCode.Focus();
                txtBarCode.SelectAll();
            }
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;

                if (lstListStock != null && lstListStock.Count > 0)
                {
                    if (WoChu.CF.BaseFrame.Util.MessageBoxEx.Question("是否提交？") == DialogResult.No) return;

                    var re = Duty.InventoryAdjustmentUIRule.NewInstance.Submit(LoginBean.Instance.USERCODE, lstListStock);
                    
                    if (re.Params)
                    {
                        lstAdjustment.Clear();
                        lstListStock.Clear(); 
                        this.adjustmentSource.DataSource = null;
                        this.dbGridAdjustment.DataSource = this.adjustmentSource;
                        this.dbGridAdjustment.Refresh();

                        this.listStockSource.DataSource = null;
                        this.dbGridListStock.DataSource = this.listStockSource;
                        this.dbGridListStock.Refresh();

                        txtLocation.Text = string.Empty;
                        txtBarCode.Text = string.Empty;
                        txtQty.Text = string.Empty;
                        txtQty3.Text = string.Empty; 
                        lblCount.Text = string.Empty;
                        lblSum.Text = string.Empty;
                        tabControlTakeStock.SelectedIndex = 0;
                        txtLocation.Focus();
                        txtLocation.SelectAll();
                        MessageBox.Show("提交成功！");
                    }
                    else
                    {
                        MessageBox.Show("提交失败！" + re.Message);
                        return;
                    }
                }
                else
                {
                    MessageBoxEx.Show("请扫描盘点数据再提交！");
                    return;
                }
            }
            catch (Exception ex)
            {
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex.Message);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        private void tabControlTakeStock_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControlTakeStock.TabPages[tabControlTakeStock.SelectedIndex].Name == tabListStock.Name)
            {
                if (lstListStock == null || lstListStock.Count <= 0) return;

                this.listStockSource.DataSource = null;
                this.listStockSource.DataSource = lstListStock;
                this.dbGridListStock.DataSource = this.listStockSource;
                this.dbGridListStock.Refresh();

                this.lblCount.Text = (from itme in lstListStock group itme by itme.LOCATIONCODE into g select new { LOCATIONCODE = g.Count() }).Count().ToString();
                this.lblSum.Text = lstListStock.Count().ToString();
            }
        }
        //显示扫描行
        private void SelectionBright(string barcode)
        { 
            if (string.IsNullOrEmpty(barcode)) return;
            int curIndex = lstAdjustment.FindIndex(x => x.GOODSBATCHCODE == barcode);
            dbGridAdjustment.Select(curIndex);
            this.dbGridAdjustment.SelectionForeColor = Color.Green;
 
        }

        //扫描排序
        private void Sort(LocationInventoryInfo curitem)
        { 
            curitem.Sort = -1; 
            lstAdjustment.Sort((x, y) =>
            {
                return x.Sort.CompareTo(y.Sort);
            });
            this.dbGridAdjustment.Refresh();
            curitem.Sort = iSort++;
        }

        
       
    }
}